var _controller;
var _args;

function listButtonInit(controller, args) {
    this._controller = controller;
    this._args = args;

    // ADD 按钮绑定事件
    $('#demo-add-row').click(function () {
        jumpTo("/" + controller + "/edit?" + args + "&redirectUrl=" + escape(encodeURI($('input[name="handle_url"]').val())));
    });

    // EDIT 按钮绑定事件
    $('#demo-edit-row').click(function () {
        var _length = 0, _id;

        $("input[type='checkbox']:checked").each(function () {
            _length++;
            _id = $(this).val();
        });

        if (_length !== 1) {
            showInfoAlert("请选择一条记录进行编辑！", null);
            return false;
        }

        jumpTo("/" + controller + "/edit?id=" + _id + "&" + args + "&redirectUrl=" + escape(encodeURI($('input[name="handle_url"]').val())));
    });

    // DELETE 按钮绑定事件
    $('#demo-delete-row').click(function () {
        showInfoAlertSelect("你确定要删除所选记录吗?", deleteFunc);
    });
}

// 删除（批量）
function deleteFunc() {
    var _selectids = "";

    $("input[type='checkbox']:checked").each(function () {
        _selectids += $(this).val() + ",";
    });

    if (_selectids === "") {
        showInfoAlert("请至少选择一条记录进行删除！", null);
        return false;
    }

    $.ajax({
        url: "/" + this._controller + "/deleteInfo",
        type: "POST",
        data: {ids: _selectids},
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");
            if (r.returnCode === "1") {
                showInfoAlert(decodeURI(r.returnMessage), refreshCurrPage);
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }
        }
    });
}

// 编辑
function editFunc(id, args) {
    if (args === 1) {
        window.location = "/" + this._controller + "/edit?id=" + id + "&" + this._args;
    } else {
        window.location = "/" + this._controller + "/edit?id=" + id;
    }
}

// 说明：先使用encodeURI对中文进行编码，然后使用escape对URL进行编码。
function editFunc(id, args, direct) {
    if (direct !== undefined && direct === 1) {
        window.location = "/" + this._controller + "/edit?id=" + id + "&" + this._args + "&redirectUrl=" + escape(encodeURI($('input[name="handle_url"]').val()));
    } else {
        window.location = "/" + this._controller + "/edit?id=" + id + "&" + this._args;
    }
}

// 复制
function copyFunc(id, args) {
    if (args === 1) {
        window.location = "/" + this._controller + "/edit?copyId=" + id + "&" + this._args + "&redirectUrl=" + escape(encodeURI($('input[name="handle_url"]').val()));
    } else {
        window.location = "/" + this._controller + "/edit?copyId=" + id + "&redirectUrl=" + escape(encodeURI($('input[name="handle_url"]').val()));
    }
}

// 查看（详情）
function showInfoFunc(id){
    window.location = "/" + this._controller + "/detail?id=" + id;
}

// 查看（标准化）
function showInfoFunc(id, tableName) {
    if (tableName === undefined) {
        window.location = "/" + this._controller + "/detail?id=" + id;
        return;
    }

    $.ajax({
        url: "/" + this._controller + "/show",
        type: "POST",
        data: {id: id, tableName: tableName},
        async: false,
        error: function (request) {
            //error
        },
        success: function (data) {
            $('.setdata-content').html(data);
        }
    });
}

//查看（自定义）
function getInfoFunc(id) {
    $.ajax({
        url: "/" + this._controller + "/getInfo",
        data: {id: id},
        type: "POST",
        async: false,
        error: function (request) {
            //error
        },
        success: function (data) {
            $('.setdata-content').html(data);
            $("#full-width-modal").modal("show");
        }
    });
}

// 删除单条记录
// 使用此方法前提条件：必须有handle_id、handle_url两个隐藏字段
// 二次确认
function gotoDeleteById(id) {
    $("input[name='handle_id']").val(id);

    showInfoAlertSelect("你确定要删除当前记录吗？", deleteFuncById);
}

function deleteFuncById() {
    $.ajax({
        url: "/" + this._controller + "/deleteById",
        type: "POST",
        data: {id: $("input[name='handle_id']").val()},
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");
            if (r.returnCode === "1") {
                showInfoAlert(decodeURI(r.returnMessage), refreshCurrPage);
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }

            $("input[name='handle_id']").val("");
        }
    });
}

// 启停用某条记录
// 使用此方法前提条件：handle_url 隐藏字段
function gotoChangeFlag(id, status) {
    $.ajax({
        url: "/" + this._controller + "/changeFlag",
        type: "POST",
        data: {id: id, status: status},
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");
            if (r.returnCode === "1") {
                showInfoAlert(decodeURI(r.returnMessage), refreshCurrPage);
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }
        }
    });
}

// 电子表格、图表共享
// 使用此方法前提条件：必须有handle_url 隐藏字段
function gotoShare(id, Status) {
    $.ajax({
        url: "/" + this._controller + "/changeShare",
        type: "POST",
        data: {id: id, status: Status},
        async: false,
        error: function (request) {
            showInfoAlert("服务器连接失败，请检查网络环境！", null);
        },
        success: function (data) {
            var r = eval("(" + data + ")");
            if (r.returnCode === "1") {
                showInfoAlert(decodeURI(r.returnMessage), refreshCurrPage);
            } else {
                showInfoAlert(decodeURI(r.returnMessage), null);
            }
        }
    });
}

// 跳转到审批
function gotoApply(id) {
    window.location = "/basic/wfapplyline/approval-line?applyId=" + id + "&redirectUrl=" + escape(encodeURI($('input[name="handle_url"]').val()));
}

// 预览
function priviewFunc(key, value) {
    window.location = "/" + this._controller + "/preview?" + key + "=" + value;
}